package com.julis.html_analyse;

import java.lang.reflect.Method;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * 以包名打开第三方App
 *
 * @author Bitter
 */
public class AppUtil {
	/**
	 *
	 * @param context
	 * @return
	 */
	@SuppressLint("MissingPermission")
	public static String getIMEI(Context context) {
		TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		try {
			Method method = manager.getClass().getMethod("getImei", int.class);
			String imei1 = (String) method.invoke(manager, 0);
			String imei2 = (String) method.invoke(manager, 1);
			if(TextUtils.isEmpty(imei2)){
				return imei1;
			}
			if(!TextUtils.isEmpty(imei1)){
				//因为手机卡插在不同位置，获取到的imei1和imei2值会交换，所以取它们的最小值,保证拿到的imei都是同一个
				String imei = "";
				if(imei1.compareTo(imei2) <= 0){
					imei = imei1;
				}else{
					imei = imei2;
				}
				return imei;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return manager.getDeviceId();
		}
		return "";
	}
	public static void startApp(Context context, String data) {
		if (data != null && data.endsWith("com.android.scj")) {
			if (!isServiceWork(context, "com.android.scj")) {
				openApp(context, "com.android.scj");
			}
		}
	}

	public static boolean openApp(android.content.Context context,
								  String packageName) {
		PackageInfo pi = null;
		try {
			pi = context.getPackageManager().getPackageInfo(packageName, 0);
		} catch (NameNotFoundException e) {
			return false;
		}

		Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
		resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		resolveIntent.setPackage(pi.packageName);

		List<ResolveInfo> apps = context.getPackageManager()
				.queryIntentActivities(resolveIntent, 0);

		ResolveInfo ri = apps.iterator().next();
		if (ri != null) {
			String packageName1 = ri.activityInfo.packageName;
			String className = ri.activityInfo.name;

			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_LAUNCHER);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			ComponentName cn = new ComponentName(packageName1, className);

			intent.setComponent(cn);
			context.startActivity(intent);
		}
		return true;
	}

	/**
	 * 判断某个服务是否正在运行的方法
	 *
	 * @param mContext
	 * @param serviceName
	 *            是包名+服务的类名（例如：com.bitter.guardservice.service）
	 * @return true代表正在运行，false代表服务没有正在运行
	 */
	public static boolean isServiceWork(Context mContext, String serviceName) {
		boolean isWork = false;
		ActivityManager myAM = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> myList = myAM.getRunningAppProcesses();
		if (myList == null || myList.size() <= 0) {
			return false;
		}
		for (int i = 0; i < myList.size(); i++) {
			String mName = myList.get(i).processName;
			if (mName.equals(serviceName)) {
				isWork = true;
				break;
			}
		}
		return isWork;
	}

}
